package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.hjpbean.NoticeBean;
import net.oschina.app.hjpbean.UserBean;
import net.oschina.app.ui.LastActivity;
import net.oschina.app.ui.LogOnActivity;
import net.oschina.app.ui.MyData;
import net.oschina.app.ui.NewsActivity;
import net.oschina.app.util.ZsfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by admin on 2017/7/13.
 */

public class MeFragment extends BaseFragment {


    @BindView(R.id.dengluxuanxiang)
    ImageView dengluxuanxiang;
    @BindView(R.id.touxiang)
    ImageView touxiang;
    @BindView(R.id.yonghuming)
    TextView yonghuming;
    @BindView(R.id.my_fengetiao)
    TextView myFengetiao;
    @BindView(R.id.jifen_count)
    TextView jifenCount;
    @BindView(R.id.shoucang_count)
    TextView shoucangCount;
    @BindView(R.id.guanzhu_count)
    TextView guanzhuCount;
    @BindView(R.id.fensi_count)
    TextView fensiCount;
    @BindView(R.id.count)
    LinearLayout count;
    @BindView(R.id.jifen)
    TextView jifen;
    @BindView(R.id.shoucang)
    TextView shoucang;
    @BindView(R.id.guanzhu)
    TextView guanzhu;
    @BindView(R.id.fensi)
    TextView fensi;
    @BindView(R.id.xiaoxi)
    TextView xiaoxi;
    @BindView(R.id.boke)
    TextView boke;
    @BindView(R.id.bianqian)
    TextView bianqian;
    @BindView(R.id.tuandui)
    TextView tuandui;
    Unbinder unbinder;
    @BindView(R.id.ll_me_text)
    LinearLayout llMeText;
    private boolean isLogIn;
    private UserBean userBean;
    private String[] shouchan={"软件","话题","代码","博客","资讯"};
    private String[] gzfs={"关注","粉丝"};
    private String[] news={"@我","评论","私信","粉丝","赞过我"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View mefragment = View.inflate(getContext(), R.layout.mefragment_layout, null);
        unbinder = ButterKnife.bind(this, mefragment);

        return mefragment;
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isLogIn = Myapp.isLogIn;
        if (isLogIn) {
            userBean = (UserBean) ZsfUtils.getObject(Myapp.getContext(),"userBean");
            dengluxuanxiang.setVisibility(View.VISIBLE);
            count.setVisibility(View.VISIBLE);
            llMeText.setVisibility(View.VISIBLE);
        }
        initView();
    }



    @OnClick({R.id.dengluxuanxiang, R.id.touxiang, R.id.my_fengetiao, R.id.shoucang_count, R.id.guanzhu_count, R.id.fensi_count, R.id.count, R.id.jifen, R.id.shoucang, R.id.guanzhu, R.id.fensi, R.id.ll_me_text, R.id.xiaoxi, R.id.boke, R.id.bianqian, R.id.tuandui})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dengluxuanxiang:
                Toast.makeText(getContext(),"暂无信息",Toast.LENGTH_SHORT).show();
                break;
            case R.id.touxiang:
                if(isLogIn){
                    Intent intent = new Intent(getActivity(), MyData.class);
                    intent.putExtra("userBean",userBean);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(getActivity(), LogOnActivity.class);
                    startActivityForResult(intent,100);
                    Toast.makeText(getContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.shoucang:
                Intent shoucang = new Intent(getActivity(), NewsActivity.class);
                shoucang.putExtra("toolbar_name","用户收藏");
                shoucang.putExtra("toolbar_center",shouchan);
                shoucang.putExtra("type",0);
                startActivity(shoucang);

                break;
            case R.id.guanzhu:
                Intent guanzhu = new Intent(getActivity(), NewsActivity.class);
                guanzhu.putExtra("toolbar_name","关注/粉丝");
                guanzhu.putExtra("toolbar_center",gzfs);
                guanzhu.putExtra("type",1);
                startActivity(guanzhu);
                break;
            case R.id.fensi:
                Intent fensi = new Intent(getActivity(), NewsActivity.class);
                fensi.putExtra("toolbar_name","关注/粉丝");
                fensi.putExtra("toolbar_center",gzfs);
                fensi.putExtra("type",2);
                startActivity(fensi);
                break;
            case R.id.xiaoxi:
                if(isLogIn){
                    Intent xiaoxi = new Intent(getActivity(), NewsActivity.class);
                    xiaoxi.putExtra("toolbar_name","消息中心");
                    xiaoxi.putExtra("toolbar_center",news);
                    xiaoxi.putExtra("type",3);
                    startActivity(xiaoxi);
                }else{
                    Intent intent = new Intent(getActivity(), LogOnActivity.class);
                    startActivityForResult(intent,100);
                    Toast.makeText(getContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.boke:
                if(isLogIn){
                    Intent boke = new Intent(getActivity(), LastActivity.class);
                    boke.putExtra("toolbar_name","用户博客列表");
                    boke.putExtra("content","暂无内容");

                    startActivity(boke);
                }else{
                    Intent intent = new Intent(getActivity(), LogOnActivity.class);
                    startActivityForResult(intent,100);
                    Toast.makeText(getContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bianqian:
                Intent bianqian = new Intent(getActivity(), LastActivity.class);
                bianqian.putExtra("toolbar_name","便签");
                bianqian.putExtra("content","暂无标签，请添加标签或者下拉");
                startActivity(bianqian);

                break;
            case R.id.tuandui:
                if(isLogIn){
                    Intent tuandui = new Intent(getActivity(), LastActivity.class);
                    tuandui.putExtra("toolbar_name","团队");
                    tuandui.putExtra("content","还没有加入的团队");
                    startActivity(tuandui);
                }else{
                    Intent intent = new Intent(getActivity(), LogOnActivity.class);
                    startActivityForResult(intent,100);
                    Toast.makeText(getContext(),"未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==100){
            if(resultCode ==102){
                Bundle extras = data.getExtras();
                userBean= (UserBean) extras.get("userBean");
                isLogIn = Myapp.isLogIn;
                if (isLogIn) {
                    dengluxuanxiang.setVisibility(View.VISIBLE);
                    count.setVisibility(View.VISIBLE);
                    llMeText.setVisibility(View.VISIBLE);
                }
                initView();
            }
        }
    }
    private void initView() {

        if(userBean!=null){
            yonghuming.setText(userBean.getName()+"");
            jifenCount.setText(userBean.getScore()+"");
            shoucangCount.setText(userBean.getFavoritecount()+"");
            guanzhuCount.setText(userBean.getFollowers()+"");
            fensiCount.setText(userBean.getFans()+"");
            Picasso.with(getContext()).load(userBean.getPortrait()).into(touxiang);
        }
    }
}

